package cn.ronghuanet.org.controller;

import cn.ronghuanet.base.exception.BusinessException;
import cn.ronghuanet.org.dto.ShopRegisterDTO;
import cn.ronghuanet.org.service.IShopService;
import cn.ronghuanet.org.domain.Shop;
import cn.ronghuanet.org.query.ShopQuery;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.insert(shop);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult deleteOne(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id")Long id)
    {
        try {
            Shop shop = shopService.selectById(id);
            return AjaxResult.success(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Shop> list = shopService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PutMapping
    public AjaxResult queryPage(@RequestBody ShopQuery query)
    {
        try {
            PageInfo<Shop> pageList = shopService.pageList(query);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }

    }

    /**
   * 批量删除
   * @return
   */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        shopService.batchDelete(ids);
        return AjaxResult.success();
    }

    /**
     * 接收参数:
     * 方式一:在Employee对象中添加一个confirmPassword字段,我们在controller用Shop对象接收就可以了
     * 方式二:另外写一个DTO来进行接收
     * 两种方式都可以
     *
     * 如果想用JSR303校验,除了在类里面给每个字段上面加注解以外,还需要在controller层的方法参数对象前面加@Valid
     * @param dto
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@Valid @RequestBody ShopRegisterDTO dto){
        // 调service做店铺入驻
        shopService.settlement(dto);
        return AjaxResult.success();
    }

    @GetMapping("/active/{shopId}")
    public AjaxResult active(@PathVariable("shopId")Long shopId){
        // 1 通过店铺id修改店铺的状态=已激活
        // 2 修改管理员的状态=正常
        return AjaxResult.success();
    }

    /**
     * @RequestParam 一般是用在Get请求的,如果参数是通过@RequestBody在请求体中传递参数的话,是用不了的
     * @param name
     * @return
     */
    @GetMapping("/get/name")
    public AjaxResult getByName(@RequestParam(value = "name",required = true) String name){
        System.out.println(name);
        return AjaxResult.success();
    }
}
